package fule.com.playandroidkotlin.ui.weight

import android.content.Context
import android.view.View
import android.view.Window
import android.widget.TextView
import fule.com.playandroidkotlin.R

class LoadingDialog(context: Context) {
    private val alertDialog: android.app.AlertDialog = android.app.AlertDialog.Builder(context).create()

    private val messageTextView: TextView

    private val window: Window?

    /**
     * 判断加载框是否在
     *
     * @return boolean值
     */
    val isShown: Boolean
        get() = alertDialog.isShowing

    init {
        alertDialog.show()

        alertDialog.setCancelable(true)
        alertDialog.setCanceledOnTouchOutside(false)

        window = alertDialog.window
        window!!.setContentView(R.layout.custom_loading_layout)

        val dm = context.resources.displayMetrics
        val w_screen = dm.widthPixels
        val h_screen = dm.heightPixels


        //设置对话框背景色为透明
        window.setBackgroundDrawableResource(R.color.transparent)
        messageTextView = window.findViewById<View>(R.id.loading_text) as TextView
    }

    /**
     * 显示progressBar
     *
     * @param context this
     * @param msgText 显示文本
     */
    constructor(context: Context, msgText: String, backgroundColor: Int) : this(context) {
        setShowText(msgText)
        window!!.setBackgroundDrawableResource(backgroundColor)
    }

    /**
     * 显示progressBar
     *
     * @param context this
     * @param msgText 显示文本
     */
    constructor(context: Context, msgText: Int) : this(context) {

        setShowText(msgText)
    }

    /**
     * 显示progressBar
     *
     * @param context this
     * @param msgText 显示文本
     */
    constructor(context: Context, msgText: String) : this(context) {
        setShowText(msgText)
    }


    /**
     * 文字
     *
     * @param resId 资源文件中
     */
    fun setShowText(resId: Int) {
        messageTextView.setText(resId)
    }

    /**
     * 文字
     *
     * @param message 加载框中的字(string)
     */
    fun setShowText(message: String) {
        messageTextView.text = message
    }

    /**
     * 显示加载框
     */
    fun showUp() {
        alertDialog.show()
    }

    /**
     * 关闭加载框
     */
    fun dismiss() {
        alertDialog.dismiss()
    }

}